package tr.edu.metu.is.mega.client.schedule;

import java.util.ArrayList;
import java.util.List;

import tr.edu.metu.is.mega.client.core.MainWindow;
import tr.edu.metu.is.mega.client.core.MegaUtil;
import tr.edu.metu.is.mega.client.resource.MEGAResourceBundle;
import tr.edu.metu.is.mega.client.resource.MEGAResources;
import tr.edu.metu.is.mega.client.service.GwtScheduleService;
import tr.edu.metu.is.mega.common.dto.ScheduleTemplateDTO;

import com.extjs.gxt.ui.client.Style.SelectionMode;
import com.extjs.gxt.ui.client.data.BaseListLoader;
import com.extjs.gxt.ui.client.data.BeanModel;
import com.extjs.gxt.ui.client.data.BeanModelReader;
import com.extjs.gxt.ui.client.data.ListLoadResult;
import com.extjs.gxt.ui.client.data.ListLoader;
import com.extjs.gxt.ui.client.data.ModelData;
import com.extjs.gxt.ui.client.data.RpcProxy;
import com.extjs.gxt.ui.client.store.ListStore;
import com.extjs.gxt.ui.client.widget.grid.CheckBoxSelectionModel;
import com.extjs.gxt.ui.client.widget.grid.ColumnConfig;
import com.extjs.gxt.ui.client.widget.grid.ColumnModel;
import com.extjs.gxt.ui.client.widget.grid.Grid;
import com.google.gwt.user.client.Element;
import com.google.gwt.user.client.rpc.AsyncCallback;

/**
 * Selection window for {@link ScheduleTemplateDTO} records.
 */
public class ScheduleTemplateSelectWindow extends MainWindow {

    private MEGAResourceBundle resourceBundle;

    private Grid<BeanModel> grid;

    /**
     * Default constructor.
     */
    public ScheduleTemplateSelectWindow() {
        super("ScheduleTemplateSelectWindow", null);
    }

    /**
     * {@inheritDoc}
     */
    @Override
    protected void onRender(Element parent, int pos) {
        super.onRender(parent, pos);
        resourceBundle = MEGAResources.RESOURCEBUNDLE;
        setHeaderVisible(false);

        add(getGrid());

    }

    /**
     * Returns grid.
     * 
     * @return grid
     */
    private Grid<BeanModel> getGrid() {

        // proxy and reader
        RpcProxy<List<ScheduleTemplateDTO>> proxy = new RpcProxy<List<ScheduleTemplateDTO>>() {

            @Override
            public void load(Object loadConfig, AsyncCallback<List<ScheduleTemplateDTO>> callback) {
                GwtScheduleService.Util.getInstance().getScheduleTemplates(callback);
            }
        };
        BeanModelReader reader = new BeanModelReader();

        ListLoader<ListLoadResult<ModelData>> loader = new BaseListLoader<ListLoadResult<ModelData>>(proxy, reader);
        ListStore<BeanModel> store = new ListStore<BeanModel>(loader);

        loader.load();

        List<ColumnConfig> columns = new ArrayList<ColumnConfig>();

        CheckBoxSelectionModel<BeanModel> cbsm = new CheckBoxSelectionModel<BeanModel>();
        cbsm.setSelectionMode(SelectionMode.SINGLE);

        columns.add(cbsm.getColumn());
        columns.add(new ColumnConfig(ScheduleTemplateDTO.FIELD_SHORT_NAME, resourceBundle.scheduleTemplateShortName(), 150));
        columns.add(new ColumnConfig(ScheduleTemplateDTO.FIELD_DESCRIPTION, resourceBundle.scheduleTemplateDescription(), 150));

        ColumnModel cm = new ColumnModel(columns);

        grid = new Grid<BeanModel>(store, cm);

        MegaUtil.decoradeGridViewWithAutoHeight(grid, true);
        return grid;
    }

    /**
     * List of items to check on grid.
     * 
     * @param selectedItems item list
     */
    public void setSelectedItems(List<ScheduleTemplateDTO> selectedItems) {

    }

    /**
     * Returns selected items from grid.
     * 
     * @return selected items
     */
    public List<ScheduleTemplateDTO> getSelectedItems() {
        List<BeanModel> selectedItems = grid.getSelectionModel().getSelectedItems();
        List<ScheduleTemplateDTO> items = new ArrayList<ScheduleTemplateDTO>(selectedItems.size());
        for (BeanModel model : selectedItems) {
            items.add((ScheduleTemplateDTO) model.getBean());
        }
        return items;
    }

}
